import _ from 'lodash';
import * as React from 'react';
import {observer} from 'mobx-react';
import {Table, Row, Col, Button, message, Modal} from 'antd';
import {PageBean} from '../../../../model/page/Page';
import {listStore,editStore,searchStore} from '../PageStore';

const columns = [{
    title: '标题',
    dataIndex: 'title',
    key:'title'
},{
    title: '业务方',
    dataIndex: 'buTitle',
    key:'buTitle'
},{
    title: '应用名称',
    dataIndex: 'appTitle',
    key:'appTitle'
},{
    title: '平台',
    dataIndex: 'platformTitle',
    key:'platformTitle'
},{
    title: '描述',
    dataIndex: 'description',
    key:'description'
},{
    title: '操作',
    key: 'operation',
    render: (text, record) => (
        <span>
            <a href="javascript:void(0);" onClick={()=>{
                editStore.load(record.id);
            }}>编辑</a>
            <span className="ant-divider" />
            <a href="javascript:void(0);" onClick={(e)=>{
                e.preventDefault();
                Modal.confirm({
                    title: '删除页面',
                    content: '确定删除该页面吗?',
                    okText: '确定',
                    cancelText: '取消',
                    onOk:()=>{
                        listStore.delete(parseInt(record.id))
                            .then((res)=>{
                                if(res.success){
                                    message.success(res.message,3);
                                    listStore.refresh(searchStore);
                                }else{
                                    message.error(res.message,3);
                                }
                            })
                    }
                });  
            }}>删除</a>
        </span>
    )
}]

@observer
export class ListTable extends React.Component<any, any> {
    componentDidMount(){
        listStore.load();
    }
    handleTableChange(me){
        return function(pagination, filters, sorter){
            let searchParams = searchStore.getValues();

            listStore.load(_.assign(searchParams,{
                pageCount: pagination.pageSize,
                pageIndex: pagination.current
            }));
        }
    }
    render() {
        return (
            <UserTable style={{paddingLeft:10}}
                    columns={columns} rowKey="id"
                    dataSource={listStore.data}
                    pagination={listStore.pagination}
                    onChange={this.handleTableChange(this)}
                />
        )
    }
}

class UserTable extends Table<PageBean<any>> {}
